package com.lianboke.link.ad.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;

import com.lianboke.link.ad.utils.KLog;

/**
 * app 安装 移除
 * Created by admin on 2017/12/13.
 */

public class AppInstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
//        PackageManager manager = context.getPackageManager();
        KLog.e("--------AppInstallReceiver----------:"+intent.getAction());
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            //安装
            String packageName = intent.getData().getSchemeSpecificPart();//获取安装成功packageName
            KLog.e("--------AppInstallReceiver----------packageName---:"+packageName);
            Intent intentInstall = new Intent();
            intentInstall.setAction("android.myapp.install.installmy");
            intentInstall.putExtra("data", packageName);
            context.sendBroadcast(intentInstall);
        }else if(intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)){
            //卸载
            String packageName = intent.getData().getSchemeSpecificPart();
            KLog.e("--------AppInstallReceiver----------packageName---:"+packageName);
            Intent removeIntent = new Intent();
            removeIntent.setAction("android.myapp.install.removemy");
            removeIntent.putExtra("data", packageName);
            context.sendBroadcast(removeIntent);
        }else if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
            //替换
            String packageName = intent.getData().getSchemeSpecificPart();
            KLog.e("--------AppInstallReceiver----------packageName---:"+packageName);
            Intent replaceIntent = new Intent();
            replaceIntent.setAction("android.myapp.install.replacemy");
            replaceIntent.putExtra("data", packageName);
            context.sendBroadcast(replaceIntent);
        }
    }
}
